

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import org.weso.lodin.appserv.RankingScoresTransformer;
import org.weso.lodin.appserv.TransformerAppServ;
import org.weso.lodin.dao.CountryDAO;
import org.weso.lodin.dao.CountryDAOImpl;
import org.weso.lodin.dao.RankingScoresObservationDAOImpl;
import org.weso.lodin.exceptions.DocumentBuilderException;
import org.weso.lodin.generated.Dataset;
import org.weso.lodin.utils.DatasetXMLBind;
import org.weso.lodin.utils.DocumentBuilderHelper;
import org.weso.lodin.utils.PrefixManager;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.vocabulary.DCTerms;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;


public class RankingScoresAppServMain {

	public static List<Dataset> getDatasetsFor(String name, int top) throws DocumentBuilderException{
		List<Dataset> datasets = new LinkedList<Dataset>();		
		datasets.add(DatasetXMLBind.getInstance().restoreIndicator(
				DocumentBuilderHelper.getDocumentFromInputStream(
						Thread.currentThread().getContextClassLoader().getResourceAsStream(
								name+".xml"))));
		for (int i = 1; i<top; i++){
			datasets.add(DatasetXMLBind.getInstance().restoreIndicator(
					DocumentBuilderHelper.getDocumentFromInputStream(
							Thread.currentThread().getContextClassLoader().getResourceAsStream(
									name+"_"+i+".xml"))));
		}
		return datasets;
	}
	
	public static Model transform2011Rankings(RankingScoresTransformer transformer, RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/2011-rankings_2",10));
			
	}
	
	public static Model transformOverallCompositeSecondaryData(RankingScoresTransformer transformer,  RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/Overall-composite-secondary-data",1));
	}
	
	public static Model transformSubindexSecondary(RankingScoresTransformer transformer, RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/Sub-Indexes-secondary_2",3));
	}
	
	
	public static Model transformComponentsSecondary(RankingScoresTransformer transformer, RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/Components-secondary_2",7));
	}
	
	public static Model transformSubIndexOverallScores(RankingScoresTransformer transformer, RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/Sub-Index-overall-scores_2",3));
	}

	public static Model transformZScores(RankingScoresTransformer transformer, RankingScoresObservationDAOImpl dao) throws FileNotFoundException, DocumentBuilderException {
		return transformer.transform(dao,getDatasetsFor("xml/rankings/z-scores",3));
	}

	public static RankingScoresObservationDAOImpl releaseDAO(){
		InputStream data = Thread.currentThread().getContextClassLoader().
				getResourceAsStream("excel/Rankings_and_scores_over_time.xls");
		return new RankingScoresObservationDAOImpl(data );
	}
	public static void main(String []args) throws IOException, DocumentBuilderException{
		String outputFile = "default-ranking-scores.ttl";		
		ResourceBundle mapPrefixesURIs = PrefixManager.getResourceBundle();	
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		InputStream inCountries = contextClassLoader.getResourceAsStream(
				TransformerAppServ.COUNTRIES_COUNTRIES_FAO_XLS);
		CountryDAO daoCountries = new CountryDAOImpl(inCountries);
		RankingScoresTransformer transformer = new RankingScoresTransformer(daoCountries);
		outputFile = "transform2011Rankings.ttl";
		RankingScoresObservationDAOImpl dao = releaseDAO();
		serializeModel(transform2011Rankings(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
		
		outputFile = "transformOverallCompositeSecondaryData.ttl";
		serializeModel(transformOverallCompositeSecondaryData(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
//		
		outputFile = "transformSubindexSecondary.ttl";
		serializeModel(transformSubindexSecondary(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
//		
		outputFile = "transformComponentsSecondary.ttl";
		serializeModel(transformComponentsSecondary(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
//		
		outputFile = "transformSubIndexOverallScores.ttl";
		serializeModel(transformSubIndexOverallScores(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
//		
		outputFile = "transformZScores.ttl";
		serializeModel(transformZScores(transformer, dao),
				mapPrefixesURIs,
				IndicatorsAppServMain.DIR_GENERATED+
				File.separator+outputFile);
	}
	
	
	
	
	static void serializeModel(Model result, ResourceBundle mapPrefixesURIs, String file)
			throws FileNotFoundException, IOException {
		addPrefixes(result, mapPrefixesURIs);
		RDFWriter w = result.getWriter("TURTLE");	
		OutputStream os= new FileOutputStream(new File(file));
		w.write(result, os,TransformerAppServ.WEBINDEX_BASE_URI);	
		os.close();
	}
	
	public static void addPrefixes(Model result, ResourceBundle mapPrefixesURIs){
		Enumeration<String> prefixes = mapPrefixesURIs.getKeys();
		while(prefixes.hasMoreElements()){
			String prefix = prefixes.nextElement();
			result.setNsPrefix(prefix, mapPrefixesURIs.getString(prefix));
		}
		//Building (not extract)
		result.setNsPrefix("dcterms", DCTerms.getURI());
		result.setNsPrefix("xsd", XSD.getURI());
		result.setNsPrefix("owl", OWL.getURI());
		result.setNsPrefix("rdf", RDF.getURI());
		result.setNsPrefix("rdfs", RDFS.getURI());
	}
}

